package com.ruoyi.web.controller.activity.red;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.activity.domian.RedPacketConfig;
import com.ruoyi.activity.service.RedPacketServiceImpl;
import com.ruoyi.activity.entity.RedPacketLimitVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 红包配置接口
 * @author chengjian 
 * @date 2021/9/9
 */
@RestController
@RequestMapping(value = "/activity/red/config")
public class RedPacketConfigController extends BaseController {

    @Resource
    private RedPacketServiceImpl redPacketService;

    @PreAuthorize("@ss.hasPermi('activity:red:config:list')")
    @GetMapping(value = "/list")
    public Page<RedPacketLimitVO> list(Integer type, Integer isNeedRobot, String beginTime, String endTime , int pageNum, int pageSize){
        return redPacketService.getConfigList(type, isNeedRobot, beginTime, endTime, pageNum, pageSize);
    }

    @PreAuthorize("@ss.hasPermi('activity:red:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getSingleConfig(@PathVariable Long id){

        return AjaxResult.success(redPacketService.getConfig(id));
    }

    @PreAuthorize("@ss.hasPermi('activity:red:config:del')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delConfig(@PathVariable Long[] ids){
        redPacketService.delConfig(Arrays.stream(ids).collect(Collectors.toList()));
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('activity:red:config:edit')")
    @PutMapping
    public AjaxResult updateConfig(@Validated @RequestBody RedPacketConfig config){
        redPacketService.updateConfig(config);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('activity:red:config:add')")
    @PostMapping
    public AjaxResult addConfig(@Validated @RequestBody RedPacketConfig config){
        redPacketService.addConfig(config);
        return AjaxResult.success();
    }
}


